﻿using Microsoft.AspNetCore.Mvc.ModelBinding;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace XXX.api
{
    public static class ReturnStd
    {
        public class ReturnJson
        {
            public string Code { get; set; }
            public string Message { get; set; }
            public object Data { get; set; }
        }
        public static ReturnJson ComJson(string responseMessage, string responseCode, object rtnData = null)
        {
            ReturnJson returnJson = new ReturnJson()
            {
                Code = responseCode,
                Message = responseMessage,
                Data = rtnData ?? string.Empty
            };
            return returnJson;
        }

        public static ReturnJson Success(object rtnData = null)
        {
            ReturnJson returnJson = new ReturnJson()
            {
                Code = "120",
                Message = string.Empty,
                Data = rtnData ?? string.Empty
            };
            return returnJson;
        }

        public static ReturnJson Error(string responseMessage, string responseCode = "400", object rtnData = null)
        {
            ReturnJson returnJson = new ReturnJson()
            {
                Code = responseCode,
                Message = responseMessage ?? string.Empty,
                Data = rtnData ?? string.Empty
            };
            return returnJson;
        }

        public static ReturnJson ModelError(ModelStateDictionary modelState, string responseCode = "400", object rtnData = null)
        {
            string messages = string.Join("; ", modelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage));

            ReturnJson returnJson = new ReturnJson()
            {
                Code = responseCode,
                Message = messages,
                Data = rtnData ?? string.Empty
            };
            return returnJson;
        }

        public static ReturnJson NotAuthorize(string responseMessage, string responseCode = "90", object rtnData = null)
        {
            ReturnJson returnJson = new ReturnJson()
            {
                Code = responseCode,
                Message = responseMessage ?? string.Empty,
                Data = rtnData ?? string.Empty
            };
            return returnJson;
        }

        public static List<T> ToList<T>(this DataTable dt)
        {
            var columnNames = dt.Columns.Cast<DataColumn>()
                .Select(c => c.ColumnName)
                .ToList();

            var properties = typeof(T).GetProperties();

            return dt.AsEnumerable().Select(row =>
            {
                var objT = Activator.CreateInstance<T>();

                foreach (var pro in properties)
                {
                    if (columnNames.Contains(pro.Name))
                        pro.SetValue(objT, row[pro.Name] == DBNull.Value ? string.Empty : row[pro.Name].ToString(), null);
                }

                return objT;
            }).ToList();
        }

    }
}
